package com.fulihui.wgj.admin.integration.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.fulihui.cloud.gameplatform.api.GameClassifyService;
import com.fulihui.cloud.gameplatform.dto.GameTagClassifyInfoDTO;
import com.fulihui.cloud.gameplatform.dto.GameTagWarehouseInfoDTO;
import com.fulihui.cloud.gameplatform.result.GameTagClassifyInfosResult;
import com.fulihui.cloud.gameplatform.result.GameTagWarehouseInfosResult;
import com.fulihui.common.conv.BeanConv;
import com.fulihui.wgj.admin.api.wxtemplate.vo.GameInfoVO;
import com.fulihui.wgj.admin.api.wxtemplate.vo.GameTagVO;
import com.fulihui.wgj.admin.integration.GamePlatformServiceClient;
import com.google.common.collect.Lists;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 游戏平台服务
 *
 * @author wang_wx
 * @date 2018-07-30
 */
@Service
public class GamePlatformServiceClientImpl implements GamePlatformServiceClient {

    @Reference(version = "1.0.0", lazy = true)
    private GameClassifyService gameClassifyService;

    @Override
    public List<GameTagVO> queryAllGameCategory() {
        GameTagClassifyInfosResult infosResult = gameClassifyService.queryAllGameClassify();
        if (infosResult != null && infosResult.getErrcode() == 0) {
            List<GameTagClassifyInfoDTO> values = infosResult.getValues();
            List<GameTagVO> gameTagVOS = BeanConv.copy(values, GameTagVO.class);
            return gameTagVOS;
        }
        return null;
    }


    @Override
    public List<GameInfoVO> queryGameInfoByIds(List<String> ids) {

        List<GameInfoVO> gameInfoVOS = Lists.newArrayList();
        if (CollectionUtils.isEmpty(ids)) {
            GameTagClassifyInfosResult infosResult = gameClassifyService.queryAllGameClassify();
            List<GameTagClassifyInfoDTO> values = infosResult.getValues();
            if (!CollectionUtils.isEmpty(values)) {
                for (GameTagClassifyInfoDTO value : values) {
                    ids.add(String.valueOf(value.getId()));
                }
            }
        }
        if (!CollectionUtils.isEmpty(ids)) {
            for (String id : ids) {
                GameTagWarehouseInfosResult infosResult = gameClassifyService.queryGamesByClassifyId(Long.valueOf(id));
                List<GameTagWarehouseInfoDTO> values = infosResult.getValues();
                if (CollectionUtils.isEmpty(values)) {
                    continue;
                }
                for (GameTagWarehouseInfoDTO value : values) {
                    GameInfoVO gameInfoVO = BeanConv.copy(value, GameInfoVO.class);
                    gameInfoVOS.add(gameInfoVO);
                }
            }
        }
        return gameInfoVOS;
    }
}
